package com.sky.test;

import com.sky.annotation.ZhtTest;
import com.sky.domain.Num;
import com.sky.domain.Student;
import com.sky.enums.test_1_6;
import org.junit.Test;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;


public class test_1 {


    public static void main(String[] args) throws ParseException {
//        Scanner in = new Scanner(System.in);
//        System.out.println("请输入任务数:");
//        int integer = 0;
//        try {
//            integer = in.nextInt();
//        } catch (Exception e) {
//            System.out.println("err");
//        }
//        System.out.println(integer);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long beginTime = System.currentTimeMillis();
// 0  0  0 0 0 0
// 32 16 8 4 2 1
        Date parse = simpleDateFormat.parse("2024-02-27 15:00:38");
        long time = 1709219293000L;
        Random random = new Random();
        int i = random.nextInt(16);
        System.out.println(32 >>> 1);
        System.out.println(new Date(time));
        long endTime = System.currentTimeMillis();
        System.out.println("使用时间: " + (endTime - beginTime) + " 毫秒");
    }
    /**计算两集合的和*/
    public static Integer addTwoNumbers(List<Integer> list1, List<Integer> list2) {
        StringBuilder result = new StringBuilder();
        int lenth;
        if(list1.size() > list2.size()){
            lenth = list1.size() - list2.size();
            for (int i = 0; i < list1.size(); i++) {
                if (lenth <= i) {
                    result.append(list1.get(i) + list2.get(i-lenth));
                }else {
                    result.append(list1.get(i));
                }
            }
        }else {
            lenth = list2.size() - list1.size();
            for (int i = 0; i < list2.size(); i++) {
                if (lenth <= i) {
                    result.append(list1.get(i-lenth) + list2.get(i));
                }else {
                    result.append(list2.get(i));
                }
            }
        }

        if (!result.toString().equals("")) {
            return Integer.parseInt(result.toString());
        }else {
            return 0;
        }
    }

    /**计算坐标上最大容积*/
    /*
    *
    *
    * ^
    * |       |
    * |       |       |
    * |       |       |
    * |       |   |   |
    * |       |   |   |
    * |___|___|___|___|________>
    * 0   1   6   3   5
    * * * * * * * * * * */
    public static int test_1_1(List<Integer> list) {
        int res = 0;//存放计算的结果
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j <= list.size(); j++) {
                if (j - i != 1) {
                  res = Math.max(res, ((list.get(i) <= list.get(j - 1) ? list.get(i) : list.get(j - 1)) * (j - i - 1)));
                }
            }
        }
        return res;
    }

    /**自由之路*/
    public static int test_1_2(String ring, String key) {
        int ringLength = ring.length();
        int num = 0;//统计数量
        char[] keys = key.toCharArray();
        if (ringLength != 1) {        //ring长度大于2
            for (char c : keys) {
                int index = ring.indexOf(c);
                if(index == -1){return -1;}
                num = index > ringLength / 2 ? num + ringLength - index + 1:num + index + 1;
            }
        }else {
            for (char c : keys) {
                if (ring.charAt(0) == c) {num = num + 1;}
            }
        }
        return num;
    }
    /**正则表达式匹配*/
    public static boolean test_1_3(String target, String format) {
//toDo
        return false;
    }
    /**百分比精度问题*/
    public static void test_1_4() {
        List<Num> listNum = new ArrayList<>();
        Num num = new Num(1, "张",123L);
        listNum.add(num);
        Num num1 = new Num(2, "张2",1329L);
        listNum.add(num1);
        Num num2 = new Num(3, "张3",3478L);
        listNum.add(num2);
        Num num3 = new Num(4, "张4",12500L);
        listNum.add(num3);
        Num num4 = new Num(5, "张5",1398L);
        listNum.add(num4);
        Num num5 = new Num(6, "张6",4567L);
        listNum.add(num5);
        Num num6 = new Num(7, "张7",98L);
        listNum.add(num6);
        //计算总数
        AtomicLong total = new AtomicLong();
        listNum.forEach(item ->
            total.addAndGet(item.getNum()));
        //计算占比
        List<BigDecimal> rat = new ArrayList<>();
        listNum.forEach(item -> {
            BigDecimal item1 = new BigDecimal(item.getNum());
            BigDecimal item2 = item1.divide(new BigDecimal(String.valueOf(total)), 5, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(3, BigDecimal.ROUND_HALF_UP);
            item.setRatio(item2);
            rat.add(item2.setScale(2,BigDecimal.ROUND_DOWN));
            item.setCompareNum((int) item2.toString().charAt(item2.toString().length()-1));
        });
        //验证是否为"1"
        BigDecimal sum1 = new BigDecimal(0);
        for (BigDecimal bigDecimal : rat) {
            sum1 = sum1.add(bigDecimal);
        }
        BigDecimal total1 = sum1.multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP);
        List<Num> collects = listNum.stream().sorted(Comparator.comparing(Num::getCompareNum)).collect(Collectors.toList());
        if (total1.compareTo(new BigDecimal(10000)) != 0) {
            if(total1.compareTo(new BigDecimal(10000)) != -1){
                BigDecimal subtract = total1.subtract(new BigDecimal(10000));
                for (int i = subtract.intValue() - 1; i >= 0; i--) {
                    collects.get(i).setRatio(collects.get(i).getRatio().setScale(2, BigDecimal.ROUND_DOWN).add(new BigDecimal(0.01)));
                }
            }else {
                BigDecimal subtract = new BigDecimal(10000).subtract(total1);
                for (int i = subtract.intValue() - 1; i >= 0; i--) {
                    collects.get(i).setRatio(collects.get(i).getRatio().setScale(2, BigDecimal.ROUND_DOWN).add(new BigDecimal(0.01)));
                }
            }
        }
        collects.forEach(item -> {
            System.out.println(item.getId() + "--" + item.getName() + "--" + item.getNum() + "--" + item.getRatio().setScale(2, BigDecimal.ROUND_DOWN));
        });
    }
    /**你可以安排的最多任务数目
     * tasks: 任务列表
     * workers: 工人列表
     * pills: 药物
     * strength: 药物增加的力量值
     */
    public static void test_1_5(List<Integer> tasks, List<Integer> workers, int pills, int strength) {
        AtomicInteger res = new AtomicInteger();
        if (!CollectionUtils.isEmpty(tasks) || !CollectionUtils.isEmpty(workers)) {
            //获取任务所需的力量值
            for (Integer task : tasks) {
                //判断是否存在工人能恰好完成任务
                if (workers.contains(task)) {
                    res.set(res.get() + 1);
                    //删除完成任务的工人
                    workers.remove(workers.indexOf(task));
                } else {
                    //创建保存力量值过剩的map,工人集合的索引作为key,差值作为值
                    Map<Integer, Integer> excessMap = new HashMap<>();
                    //创建保存力量值不够的map
                    Map<Integer, Integer> insufficientMap = new HashMap<>();
                    for (int i = 0; i < workers.size(); i++) {
                        int num = workers.get(i) - task;
                        if (num > 0) {
                            //存在力量值过剩的工人
                            excessMap.put(i, num);
                        } else {
                            //存在力量值不够的工人
                            insufficientMap.put(i, num);
                        }
                    }
                    //先处理力量值不够的map
                    if (!CollectionUtils.isEmpty(insufficientMap) && pills > 0) {
                        //获取没map中值最小的值
                        int min = Integer.MAX_VALUE;
                        int index = -1;
                        for (Map.Entry<Integer, Integer> entry : insufficientMap.entrySet()) {
                            if (entry.getValue() < min) {
                                if ((entry.getValue() + strength) >= 0) {//最小值必须满足加上力量值的值是否满足任务要求
                                    min = entry.getValue();
                                    //获取力量值不够的工人索引
                                    index = entry.getKey();
                                }
                            }
                        }
                        //删除该工人,标识该任务交给该工人
                        if (index != -1) {
                            workers.remove(index);
                            pills = pills - 1;
                            res.set(res.get() + 1);
                            continue;
                        }
                    }
                    if (!CollectionUtils.isEmpty(excessMap)){ //处理力量值过剩的map
                        //获取没map中值最小的值
                        int min = Integer.MAX_VALUE;
                        for (Map.Entry<Integer, Integer> entry : excessMap.entrySet()) {
                            if (entry.getValue() < min) {
                                min = entry.getValue();
                            }
                        }
                        //获取力量值过剩的工人索引
                        int finalMin = min;
                        int index = -1;
                        for (Integer key:excessMap.keySet()){
                            if (excessMap.get(key) == finalMin) {
                                index = key;
                            }
                        }
                        //删除该工人,标识该任务交给该工人
                        if (index != -1) {
                            workers.remove(index);
                            res.set(res.get() + 1);
                        }
                    }
                }
            }
        }
        System.out.println("完成的任务: " + res);
    }

    /**枚举的简单玩法*/
    public static void test_1_6() {
        test_1_6 Long = test_1_6.LONG;
        test_1_6 aDouble = test_1_6.DOUBLE;
        test_1_6 string = test_1_6.STRING;
        System.out.println("长整型: " + Long.test_1_6_1());
        System.out.println("double类型: " + aDouble.test_1_6_1());
        System.out.println("字符串类型: " + string.test_1_6_1());
    }
    @Test
    /**Redis的简单使用 -- Jedis*/
    public void test_1_7() {
        /**
         * 使用Jedis操作Redis
         */
        //1 获取连接
        Jedis jedis = new Jedis("localhost", 6379);

        //2 执行具体的操作
//        jedis.set("username", "sky");

        //输出打印
        String value = jedis.get("username");
        System.out.println(value);

        //删除操作
        //jedis.del("username");

        //哈希存储
        jedis.hset("myHash", "addr", "bj");
        //输出打印
        String hValue = jedis.hget("myHash", "addr");
        System.out.println(hValue);

//        //keys * 输出打印
//        Set<String> keys = jedis.keys("*");
//        for (String key : keys) {
//            System.out.println(key);
//        }

        //3 关闭连接
        jedis.close();
    }

    @ZhtTest
    /**注解和aop的简单玩法*/
    public static void test_1_8() {

    }
    @Test
    public void test_1_9() {
        Student student = new Student();
        student.setName("sky11");
        Student student1 = new Student();
        student1.setName("sky111");
        Student student2 = new Student();
        student2.setName("sky2");
        Student student3 = new Student();
        student3.setName("4sky");
        List<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.sort(Comparator.comparing(Student::getName));
        for (Student student4 : list) {
            System.out.println(student4.getName());
        }
    }
}
